﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;

namespace Future.FormBase
{
    public partial class frmBaseChild : frmBase, IMdiChildForm, ISystemButtons
    {
        /// <summary>
        /// 父窗体的Toolbar组件
        /// </summary>
        private IToolbarRegister _ToolbarRegister = null;
        /// <summary>
        /// 初始化数组
        /// </summary>
        private IButtonList _buttons = new ButtonList();
        /// <summary>
        /// 子窗体观察者列表
        /// </summary>
        private IList _observer = new ArrayList();
        /// <summary>
        /// 子窗体的按钮数组
        /// </summary>
        private IButtonInfo[] _SystemButtons = null;
        /// <summary>
        /// 窗体是否正在关闭状态
        /// </summary>
        private bool _IsClosing = false;


        public frmBaseChild()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 可以通过外部调用该方法重新设置按钮权限.
        /// </summary>
        public virtual void SetButtonAuthority() { }

        #region IMdiChildForm 成员

        public IToolbarRegister ToolbarRegister
        {
            get { return _ToolbarRegister; }
            set { _ToolbarRegister = value; }
        }

        public void RegisterToolBar(IToolbarRegister toolBarRegister)
        {
            //this.Buttons是当前窗体的按钮数组。
            toolBarRegister.RegisterButton(this.Buttons.ToList());
        }

        /// <summary>
        /// 注册子窗体观察者
        /// </summary>   
        public void RegisterObserver(IObserver[] observers)
        {
            foreach (IObserver o in observers) _observer.Add(o);
        }

        /// <summary>
        /// 子窗体数组按钮
        /// </summary>
        public IButtonList Buttons
        {
            get { return _buttons; }
        }

        public virtual void InitButtons()
        {
            IButtonInfo[] bi = this.GetSystemButtons();
            _buttons.AddRange(bi);
        }

        public bool IsClosing
        {
            get { return _IsClosing; }
            set { _IsClosing = value; }
        }

        #endregion

        #region ISystemButtons 成员

        /// <summary>
        /// 系统按钮列表。注：子窗体享用系统按钮，如帮助/关闭窗体常用功能。
        /// </summary>        
        public virtual IButtonInfo[] GetSystemButtons()
        {
            if (_SystemButtons == null)
            {
                _SystemButtons = new IButtonInfo[2];
                _SystemButtons[0] = this.ToolbarRegister.CreateButton("btnHelp", "帮助",
                    Resource._24_Help.ToBitmap(), new Size(57, 28), this.DoHelp);
                _SystemButtons[1] = this.ToolbarRegister.CreateButton("btnClose", "关闭",
                    Resource._24_Exit.ToBitmap(), new Size(57, 28), this.DoClose);
            }
            return _SystemButtons;
        }

        public virtual void DoHelp(IButtonInfo sender)
        {
            
        }

        public virtual void DoClose(IButtonInfo sender)
        {
            this.Close();
        }

        #endregion

        private void frmBaseChild_Activated(object sender, EventArgs e)
        {
            this.RegisterToolBar(this.ToolbarRegister);
            this.NotifyObserver();
        }
        /// <summary>
        /// 通知观察者进行更新
        /// </summary>
        private void NotifyObserver()
        {
            foreach (IObserver o in _observer) if (o != null) o.Notify();
        }

        private void frmBaseChild_FormClosing(object sender, FormClosingEventArgs e)
        {
            _IsClosing = true;
        }
    }
}
